package server.imp;


import java.util.LinkedList;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import data.ChatServerInfo;
import server.Server;

public class ServerContainer implements Server{
	
	private final static Logger logger = LogManager.getLogger(ServerContainer.class);
	
	private List<Server> servers = new LinkedList<>();
	
	public void add(Server server){
		servers.add(server);
	}
	
	@Override
	public void server() {
		for (int i = 0; i <servers.size(); i++) {
			servers.get(i).server();
		}
	}

	@Override
	public void shutdown() {
		for (int i = 0; i <servers.size(); i++) {
			servers.get(i).shutdown();
			try {
				Server server = servers.get(i).getClass().newInstance();
				servers.remove(i);
				servers.add(i,server);
				ChatServerInfo info = ChatServerInfo.getInstance();
				info.setRunning(false);
				info.setTime(System.currentTimeMillis());
			} catch (InstantiationException | IllegalAccessException e) {
				logger.error(e);
			}
			
		}
	}
	
	public void start(){
		for (int i = 0; i <servers.size(); i++) {
			servers.get(i).start();
			ChatServerInfo info = ChatServerInfo.getInstance();
			info.setRunning(true);
			info.setTime(System.currentTimeMillis());
		}
	}
	

}
